package com.sakura.mall.coupon.seckillSkuNotice.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.coupon.seckillSkuNotice.domain.entity.SeckillSkuNoticeEntity;
import com.sakura.mall.coupon.seckillSkuNotice.domain.request.AddSeckillSkuNoticeRequest;
import com.sakura.mall.coupon.seckillSkuNotice.domain.request.UpdateSeckillSkuNoticeRequest;
import com.sakura.mall.coupon.seckillSkuNotice.domain.request.SeckillSkuNoticePageRequest;
import com.sakura.mall.coupon.seckillSkuNotice.domain.vo.SeckillSkuNoticeVo;
import com.sakura.mall.coupon.seckillSkuNotice.mapper.SeckillSkuNoticeMapper;
import com.sakura.mall.coupon.seckillSkuNotice.service.SeckillSkuNoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 秒杀商品通知订阅 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class SeckillSkuNoticeServiceImpl extends ServiceImpl<SeckillSkuNoticeMapper, SeckillSkuNoticeEntity> implements SeckillSkuNoticeService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddSeckillSkuNoticeRequest request) {
        // todo 其他校验逻辑
        SeckillSkuNoticeEntity seckillSkuNotice = request.convert(SeckillSkuNoticeEntity.class);
        this.save(seckillSkuNotice);
    }

    @Override
    public Page<SeckillSkuNoticeVo> queryPage(SeckillSkuNoticePageRequest request) {
        IPage<SeckillSkuNoticeEntity> qp = request.toPage();
        LambdaQueryWrapper<SeckillSkuNoticeEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<SeckillSkuNoticeVo> ret = qp.convert(entity -> entity.convert(SeckillSkuNoticeVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateSeckillSkuNoticeRequest request) {
        SeckillSkuNoticeEntity entity = request.convert(SeckillSkuNoticeEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

